// pages/me/me.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,
        isHide: false,
        userInfo: '',
        has_integral: 0,
        used_integral: 0,
        total_integral: 0,
        clock_days: 0,
        user_id: '',
        powerList: [{
            title: '数据库',
            tip: '安全稳定的文档型数据库',
            showItem: true,
            item: [{
                title: '查询操作记录',
                page: 'record'
                }, {
                title: '任务管理功能',
                page: 'taskmanage'
                }, {
                title: '奖励管理功能',
                page: 'giftmanage'
                }
            ]
          }],
    },

    jumpPage(e) {
        console.log(e.currentTarget.dataset.page)
        wx.navigateTo({
          url: `/pages/${e.currentTarget.dataset.page}/${e.currentTarget.dataset.page}`,
        });
      },
    
    /**获取用户信息*/
    userInfoShow() {
        let user = wx.getStorageSync('user')
        if(user) {
            wx.cloud.callFunction({
                name: 'babyCloudFuncs',
                data: {
                    type: 'getUserInfo',
                    data: {
                        nickName: user.nickName
                    }
                }
            }).then((res) => {
                let tmp = res.result.data
                // console.log(tmp)
                if (tmp.length != 0) {
                    this.setData({
                        has_integral: tmp[0].has_integral,
                        used_integral: tmp[0].used_integral,
                        total_integral: tmp[0].total_integral,
                        clock_days: tmp[0].clock_days,
                        user_id: tmp[0]._id
                    })
                }  
            })
        }
    },
    /**
     * 注册用户
     */
    register (nickname) {
        // let user = wx.getStorageSync('user') 
        wx.cloud.callFunction({
            name: 'babyCloudFuncs',
            data: {
                type: 'getUserInfo',
                data: {
                    nickName: nickname
                }
            }
        }).then((res) => {
            console.log('res',res)
            if (res.result.data.length == 0) {
                // 数据库中没该用户信息 则注册
                wx.cloud.callFunction({
                    name: 'babyCloudFuncs',
                    data: {
                        type: 'registerUser',
                        data: {
                            nickName: nickname
                        }
                    }
                })
            } 
        })
        
    },
    /**
     * 登录
     */
    login() {
        wx.getUserProfile({
          desc: '必须授权才可',
          success: res => {
              let user = res.userInfo
              this.register(user.nickName)
              this.userInfoShow()
              wx.setStorageSync('user', user)
              wx.setStorageSync('isLogin', true)
              wx.setStorageSync('isHide', true)
              var app = new getApp()
              app.globalData.userInfo = user
              this.setData({
                isLogin: true,
                isHide: true
              })
          },
          fail: res => {
              wx.showModal({
                title: '警告',
                content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',   
                showCancel: false 
              })
          }
        })
    },
    /**
     * 退出登录
     */
    logout() {
        wx.showModal({
          title: '提示',
          content:  "你确定要退出吗",
          success: res => {
              if (res.confirm) {
                  this.setData({
                      isHide: false,
                      isLogin: false,
                      user: null
                  })
                  // 清空缓存
                  wx.setStorageSync('user', null)
                  wx.setStorageSync('isLogin', false)
                  wx.setStorageSync('isHide', false)
                  var app = new getApp()
                  app.globalData.userInfo = null
              } else {
                  console.log('取消退出登录')
              }
          }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let user = wx.getStorageSync('user')
        let isLogin = wx.getStorageSync('isLogin')
        let isHide = wx.getStorageSync('isHide')
        this.setData({
            userInfo: user,
            isLogin: isLogin,
            isHide: isHide

        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let user = wx.getStorageSync('user')
        if (user) {
            this.userInfoShow()
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})